package com.blockmeta.bbs.immodule.widget;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.blockmeta.bbs.immodule.R;

import java.util.List;


public class Popup_From_Bottom
{
	private PopupWindow m_popup;

	private View showView;

	private OnDismissListener m_Listener;

	private List<String> actions;

	private Context mContext;

	private boolean mChooseOrderType = true;

	public Popup_From_Bottom(Context pContext, View pShowView, AdapterView.OnItemClickListener pOnItemClick,
							 List<String> actions, OnDismissListener p_Listener)
	{
		this.showView = pShowView;
		this.actions = actions;
		this.mContext = pContext;
		this.m_Listener = p_Listener;
		View view = LayoutInflater.from(pContext).inflate(R.layout.layout_popup_from_bottom, null, false);


		view.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View view)
			{
				dismiss();
			}
		});

		ListView mListView = (ListView) view.findViewById(R.id.lv_action);
		mListView.setAdapter(new MyAdapter());
		mListView.setOnItemClickListener(pOnItemClick);
		m_popup = new PopupWindow();
		m_popup.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
		m_popup.setFocusable(true);
		m_popup.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
		m_popup.setContentView(view);
		m_popup.setOnDismissListener(new PopupWindow.OnDismissListener()
		{
			@Override
			public void onDismiss()
			{
				if (null != m_Listener)
				{
					m_Listener.onDismissListener();
				}
			}
		});
	}

	public void dismiss()
	{
		if (null != m_popup && m_popup.isShowing())
		{
			m_popup.dismiss();
		}
	}

	public void show()
	{
		if (null != m_popup)
		{
			m_popup.showAtLocation(showView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
		}
	}

	public interface OnDismissListener
	{
		void onDismissListener();
	}

	class MyAdapter extends BaseAdapter
	{

		@Override
		public int getCount()
		{
			return null == actions ? 0 : actions.size();
		}

		@Override
		public Object getItem(int position)
		{
			return null == actions ? 0 : actions.get(position);
		}

		@Override
		public long getItemId(int position)
		{
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			ViewHolder mHolder;
			if (null == convertView)
			{
				convertView = LayoutInflater.from(mContext).inflate(R.layout.item_popup_from_bottom, parent, false);
				mHolder = new ViewHolder();
				mHolder.tvAction = (TextView) convertView.findViewById(R.id.tv_acition);
				convertView.setTag(mHolder);
			}
			else
			{
				mHolder = (ViewHolder) convertView.getTag();
			}

			mHolder.tvAction.setText(actions.get(position));

			return convertView;
		}

		class ViewHolder
		{
			TextView tvAction;
		}
	}
}
